<%@ Page language="vb" CodeFile="editGarage.aspx.vb" Inherits="editGarage_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        garageID = Request.QueryString("garageID")
        garageID = CLng(garageID)
        dbOpen()
        RecSet = Conn.Execute("Select g.garage, g.street, g.zipcode, g.city, g.areacode, g.telephone, g.fax, g.email, g.note, c.country, (Select Count(c.garageID) From tblClaim c Where c.garageID = g.garageID) As garageCount From (tblGarage g INNER JOIN tblCountry c ON c.countryID = g.countryID) Where g.garageID = " & garageID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchGarage.aspx")
        Else
            garage = RecSet.Fields("garage").Value
            street = RecSet.Fields("street").Value
            zipcode = RecSet.Fields("zipcode").Value
            city = RecSet.Fields("city").Value
            areacode = RecSet.Fields("areacode").Value
            telephone = RecSet.Fields("telephone").Value
            fax = RecSet.Fields("fax").Value
            email = RecSet.Fields("email").Value
            note = RecSet.Fields("note").Value
            country = RecSet.Fields("country").Value
            garageCount = RecSet.Fields("garageCount").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        'Set RecSet = Conn.Execute("Select c.countryCurrency, (Select Count(c.claimID) From tblClaim c Where c.garageID = g.garageID) As claimCount, (Select Sum(c.paymentAmount) From tblClaim c Where c.garageID = g.garageID) As claimSum From (tblGarage g INNER JOIN tblCountry c ON c.countryID = g.countryID) Where g.garageID = " & garageID & "")
        RecSet = Conn.Execute("Select c.countryCurrency, (Select Count(c.claimID) From tblClaim c Where c.garageID = g.garageID) As claimCount, (Select Sum(c.paymentAmount) From tblClaim c Where c.garageID = g.garageID) As claimSum, (Select Sum(cm.costPerHour*cm.numberOfHours) From (tblClaimMoment cm INNER JOIN tblClaim c ON c.claimID = cm.claimID) Where c.garageID = g.garageID) As claimMomentSum, (Select Sum(cp.costPerPart*cp.numberOfParts) From (tblClaimPart cp INNER JOIN tblClaim c ON c.claimID = cp.claimID) Where c.garageID = g.garageID) As claimPartSum, (Select Sum(cs.costPerService*cs.numberOfService) From (tblClaimService cs INNER JOIN tblClaim c ON c.claimID = cs.claimID) Where c.garageID = g.garageID) As claimServiceSum, (Select Sum(c.consumptionMaterialAmount) From tblClaim c Where c.garageID = g.garageID) As consumptionMaterialSum, (Select Sum(cm.authorisedCostPerHour*cm.authorisedNumberOfHours) From (tblClaimMoment cm INNER JOIN tblClaim c ON c.claimID = cm.claimID) Where c.garageID = g.garageID) As authorisedClaimMomentSum, (Select Sum(cp.authorisedCostPerPart*cp.authorisedNumberOfParts) From (tblClaimPart cp INNER JOIN tblClaim c ON c.claimID = cp.claimID) Where c.garageID = g.garageID) As authorisedClaimPartSum, (Select Sum(cs.authorisedCostPerService*cs.authorisedNumberOfService) From (tblClaimService cs INNER JOIN tblClaim c ON c.claimID = cs.claimID) Where c.garageID = g.garageID) As authorisedClaimServiceSum, (Select Sum(c.authorizedConsumptionMaterialAmount) From tblClaim c Where c.garageID = g.garageID) As authorisedConsumptionMaterialSum From (tblGarage g INNER JOIN tblCountry c ON c.countryID = g.countryID) Where g.garageID = " & garageID & "")
        If Not RecSet.EOF Then 
            countryCurrency = RecSet.Fields("countryCurrency").Value
            claimCount = RecSet.Fields("claimCount").Value
            claimSum = RecSet.Fields("claimSum").Value
            claimMomentSum = RecSet.Fields("claimMomentSum").Value
            claimPartSum = RecSet.Fields("claimPartSum").Value
            claimServiceSum = RecSet.Fields("claimServiceSum").Value
            consumptionMaterialSum = RecSet.Fields("consumptionMaterialSum").Value
            authorisedClaimMomentSum = RecSet.Fields("authorisedClaimMomentSum").Value
            authorisedClaimPartSum = RecSet.Fields("authorisedClaimPartSum").Value
            authorisedClaimServiceSum = RecSet.Fields("authorisedClaimServiceSum").Value
            authorisedConsumptionMaterialSum = RecSet.Fields("authorisedConsumptionMaterialSum").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(claimSum) Then 
            claimSum = 0
        End IF
        If IsDBNull(claimMomentSum) Then 
            claimMomentSum = 0
        End IF
        If IsDBNull(claimPartSum) Then 
            claimPartSum = 0
        End IF
        If IsDBNull(claimServiceSum) Then 
            claimServiceSum = 0
        End IF
        If IsDBNull(consumptionMaterialSum) Then 
            consumptionMaterialSum = 0
        End IF
        If IsDBNull(authorisedClaimMomentSum) Then 
            authorisedClaimMomentSum = 0
        End IF
        If IsDBNull(authorisedClaimPartSum) Then 
            authorisedClaimPartSum = 0
        End IF
        If IsDBNull(authorisedClaimServiceSum) Then 
            authorisedClaimServiceSum = 0
        End IF
        If IsDBNull(authorisedConsumptionMaterialSum) Then 
            authorisedConsumptionMaterialSum = 0
        End IF
        claimAmount = claimMomentSum + claimPartSum + claimServiceSum + consumptionMaterialSum
        authorisedClaimAmount = authorisedClaimMomentSum + authorisedClaimPartSum + authorisedClaimServiceSum + authorisedConsumptionMaterialSum
        If CInt(claimAmount) = 0 Or CInt(authorisedClaimAmount) = 0 Then 
            authorisedPercentage = 0
        Else
            authorisedPercentage = CDbl(authorisedClaimAmount) / CDbl(claimAmount)
            authorisedPercentage = authorisedPercentage * 100
        End IF
        If CDbl(garageCount) = 0 Then 
%>
<script type="text/javascript">
  function deleteCheck() { 
	if (window.confirm('OBS! Are you sure you want to delete this garage?')) {
		location.href='editGarage.aspx?garageID=<%= garageID %>&do=delete';
	}
	else {
		return false;
	}
  }
  </script>
<%
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""garage"", ""==''""],"
        strJS = strJS + "[""street"", ""==''""],"
        strJS = strJS + "[""zipcode"", ""==''""],"
        strJS = strJS + "[""city"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EDIT GARAGE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='editGarage.aspx?do=save&garageID=" & garageID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='country' id='country' value='" & country & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Garage:</td><td align='right'><input type='text' name='garage' id='garage' value='" & garage & "' size='40' maxlength='50'></td><td><div id='error_garage' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Street:</td><td align='right'><input type='text' name='street' id='street' value='" & street & "' size='40' maxlength='50'></td><td><div id='error_street' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Zipcode:</td><td align='right'><input type='text' name='zipcode' id='zipcode' value='" & zipcode & "' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_zipcode' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>City:</td><td align='right'><input type='text' name='city' id='city' value='" & city & "' size='40' maxlength='50'></td><td><div id='error_city' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Areacode:</td><td align='right'><input type='text' name='areacode' id='areacode' value='" & areacode & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Telephone:</td><td align='right'><input type='text' name='telephone' id='telephone' value='" & telephone & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Fax:</td><td align='right'><input type='text' name='fax' id='fax' value='" & fax & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Email:</td><td align='right'><input type='text' name='email' id='email' value='" & email & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Note:</td><td align='right'><input type='text' name='note' id='note' value='" & note & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CDbl(garageCount) = 0 Then 
            Response.Write("<input type='button' value='Delete garage' onclick=""{return deleteCheck();} return false;"">&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save changes'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>GARAGE STATISTICS<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of claims reported:</td><td align='right'>" & claimCount & "</td></tr>")
        Response.Write("<tr><td>Total requested claim amount (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(claimAmount), 2) & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Total authorized claim amount (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(authorisedClaimAmount), 2) & "</td></tr>")
        Response.Write("<tr><td>Total authorized percentage (%):</td><td align='right'>" & FormatNumber(authorisedPercentage, 2) & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Total claim cost (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(claimSum), 2) & "</td></tr>")
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        garageID = Request.QueryString("garageID")
        garage = Request.Form("garage")
        street = Request.Form("street")
        zipcode = Request.Form("zipcode")
        city = Request.Form("city")
        areacode = Request.Form("areaCode")
        telephone = Request.Form("telephone")
        fax = Request.Form("fax")
        email = Request.Form("email")
        note = Request.Form("note")
        userID = fragusLeasingUserID
        garageID = CLng(garageID)
        If Len(garage) > 0 Then 
            garage = Trim(garage)
            garage = Replace(CStr(garage), "'", "")
            garage = Replace(CStr(garage), """", "")
            garage = Replace(CStr(garage), "<", "")
            garage = Replace(CStr(garage), ">", "")
            garage = Replace(CStr(garage), "=", "")
        End IF
        If Len(street) > 0 Then 
            street = Trim(street)
            street = Replace(CStr(street), "'", "")
            street = Replace(CStr(street), """", "")
            street = Replace(CStr(street), "<", "")
            street = Replace(CStr(street), ">", "")
            street = Replace(CStr(street), "=", "")
        End IF
        If Len(zipcode) > 0 Then 
            zipcode = Trim(zipcode)
            zipcode = Replace(CStr(zipcode), "'", "")
            zipcode = Replace(CStr(zipcode), """", "")
            zipcode = Replace(CStr(zipcode), "<", "")
            zipcode = Replace(CStr(zipcode), ">", "")
            zipcode = Replace(CStr(zipcode), "=", "")
        End IF
        If Len(city) > 0 Then 
            city = Trim(city)
            city = Replace(CStr(city), "'", "")
            city = Replace(CStr(city), """", "")
            city = Replace(CStr(city), "<", "")
            city = Replace(CStr(city), ">", "")
            city = Replace(CStr(city), "=", "")
        End IF
        If Len(areacode) > 0 Then 
            areacode = Trim(areacode)
            areacode = Replace(CStr(areacode), "'", "")
            areacode = Replace(CStr(areacode), """", "")
            areacode = Replace(CStr(areacode), "<", "")
            areacode = Replace(CStr(areacode), ">", "")
            areacode = Replace(CStr(areacode), "=", "")
        End IF
        If Len(telephone) > 0 Then 
            telephone = Trim(telephone)
            telephone = Replace(CStr(telephone), "'", "")
            telephone = Replace(CStr(telephone), """", "")
            telephone = Replace(CStr(telephone), "<", "")
            telephone = Replace(CStr(telephone), ">", "")
            telephone = Replace(CStr(telephone), "=", "")
        End IF
        If Len(fax) > 0 Then 
            fax = Trim(fax)
            fax = Replace(CStr(fax), "'", "")
            fax = Replace(CStr(fax), """", "")
            fax = Replace(CStr(fax), "<", "")
            fax = Replace(CStr(fax), ">", "")
            fax = Replace(CStr(fax), "=", "")
        End IF
        If Len(email) > 0 Then 
            email = Trim(email)
            email = Replace(CStr(email), "'", "")
            email = Replace(CStr(email), """", "")
            email = Replace(CStr(email), "<", "")
            email = Replace(CStr(email), ">", "")
            email = Replace(CStr(email), "=", "")
        End IF
        If Len(note) > 0 Then 
            note = Trim(note)
            note = Replace(CStr(note), "'", "")
            note = Replace(CStr(note), """", "")
            note = Replace(CStr(note), "<", "")
            note = Replace(CStr(note), ">", "")
            note = Replace(CStr(note), "=", "")
        End IF
        dbOpen()
        Conn.Execute("Update tblGarage Set userID = " & userID & ", garage = '" & garage & "', street = '" & street & "', zipcode = '" & zipcode & "', city = '" & city & "', areacode = '" & areacode & "', telephone = '" & telephone & "', fax = '" & fax & "', email = '" & email & "', note = '" & note & "' Where garageID = " & garageID & "")
        dbClose()
        Response.Redirect("editGarage.aspx?subDo=%20-%20SAVED&garageID=" & garageID & "")
    ElseIf Request.QueryString("do") = "delete" Then 
        garageID = Request.QueryString("garageID")
        garageID = CLng(garageID)
        dbOpen()
        Conn.Execute("Delete From tblGarage Where garageID = " & garageID & "")
        dbClose()
        Response.Redirect("addGarage.aspx?subDo=%20-%20DELETED")
    End IF
%>
